<template>
    <div class="integral-detail">
        <div class="pic-img">
            <img :src="integralMallDetail.couponpic">
        </div>
        <div class="detail-content-wrapper">
            <div class="info-wrapper">
                <van-row>
                    <van-col span="24">
                        <div class="coupon-name">{{integralMallDetail.couponName}}</div>
                    </van-col>
                </van-row>
                <van-row>
                    <van-col span="12">
                        <div class="coupon-price">市场价：￥{{integralMallDetail.marketPrice}}</div>
                    </van-col>
                    <van-col span="12">
                        <div class="receive-num">已售数量：{{integralMallDetail.receiveNum}}</div>
                    </van-col>
                </van-row>
                <van-row>
                    <van-col span="12">
                        <div class="need-integral">消耗积分：{{integralMallDetail.needIntegral}}</div>
                    </van-col>
                    <van-col span="12">
                        <div class="exchange-btn">
                            <van-button round type="primary" @click="showPopUp">立即兑换</van-button>
                        </div>
                    </van-col>
                </van-row>
                <!--<van-row>-->
                <!--<van-col span="24">-->
                <!--<div class="receive-num">已售{{integralMallDetail.receiveNum}}</div>-->
                <!--</van-col>-->
                <!--</van-row>-->
            </div>
            <div class="rule-wrapper">
                <van-row>
                    <van-col span="24">
                        <div class="date-text">有效期：</div>
                    </van-col>
                </van-row>
                <van-row>
                    <van-col span="24">
                        <div class="date-range">{{integralMallDetail.receiveStartTime}} -
                            {{integralMallDetail.receiveEndTime}}
                        </div>
                    </van-col>
                </van-row>
                <van-row>
                    <van-col span="24">
                        <div class="rules-text">使用规则：</div>
                    </van-col>
                </van-row>
                <van-row>
                    <van-col span="24">
                        <div class="rules-info">
                            <section v-html="integralMallDetail.useRules"></section>
                        </div>
                    </van-col>
                </van-row>
            </div>
        </div>
        <div class="popup-wrapper">

            <van-popup v-model="popupShow" position="bottom" :lock-scroll="true">
                <div class="integral-cart-wrapper">
                    <div class="title-wrapper">
                        <div class="title">
                            积分兑换
                        </div>
                        <div class="cross-btn" @click="showPopUp">
                            <van-icon name="cross"/>
                        </div>
                    </div>

                    <div class="info-wrapper">
                        <van-row>
                            <van-col span="6">
                                <div class="logo">
                                    <img :src="integralMallDetail.logo">
                                </div>
                            </van-col>
                            <van-col span="18">
                                <div class="name">
                                    <span> {{integralMallDetail.couponName}}</span>
                                </div>
                            </van-col>
                        </van-row>
                    </div>
                    <div class="buy-num-wrapper">
                        <van-row>
                            <van-col span="12">
                                <div class="text">
                                    购买数量
                                </div>
                            </van-col>
                            <van-col span="12">
                                <div class="count">
                                    <van-stepper v-model="exchangeCount" :disable-input="true" :integer="true" min="1"
                                                 :max="exchangeNum"/>
                                </div>
                            </van-col>
                        </van-row>
                    </div>
                    <div class="buy-btn-wrapper">
                        <van-button round type="primary" @click="integralMallCreate">立即兑换</van-button>
                    </div>
                </div>
            </van-popup>
        </div>
    </div>
</template>

<script>
    import Cookies from 'js-cookie'
    import {isDev, getScaleHeight} from "../../utils/tools";
    import {Toast} from 'vant';


    export default {
        name: "IntegralDetail",
        inject: ['reload'],
        components: {},
        data() {
            return {
                activityCpId: '',
                integralMallDetail: {},
                popupShow: false,
                exchangeCount: 1,
                perReceiveCondition: {
                    perdayLimit: 0,//每日
                    peroneLimit: 0, //每人
                    perLimit: 0  //每人每日
                }
            }
        },
        mounted() {
            getScaleHeight(".pic-img")
            this.token = Cookies.get("token")
            this.activityCpId = this.$route.query.id
            isDev && console.log(this.token);
            this.fetchIntegralDetail()
        },
        computed: {
            exchangeStatus() {
                if (this.perReceiveCondition.perdayLimit >= this.integralMallDetail.perdayLimit) return false //每日 (最大限制)
                if (this.perReceiveCondition.peroneLimit >= this.integralMallDetail.peroneLimit) return false //每人
                if (this.perReceiveCondition.perLimit >= this.integralMallDetail.perLimit) return false //每人每日
                return true
            },
            exchangeNum() {
                let exchangeNum = 5;
                let list = []
                if (this.integralMallDetail.peroneLimit && this.integralMallDetail.peroneLimit > 0) {
                    list.push(this.integralMallDetail.peroneLimit)
                }
                if (this.integralMallDetail.perdayLimit && this.integralMallDetail.perdayLimit > 0) {
                    list.push(this.integralMallDetail.perdayLimit)
                }
                if (this.integralMallDetail.perLimit && this.integralMallDetail.perLimit > 0) {
                    list.push(this.integralMallDetail.perLimit)
                }
                if (list.length > 0) {
                    exchangeNum = Math.min(...list)
                }
                return exchangeNum;
            }
        },
        methods: {
            fetchIntegralDetail() {
                this.$api.integralmall.integralMallDetail({id: this.activityCpId, openId: this.token}).then(res => {
                    // isDev&&console.log(res);
                    if (res.code == 0) {
                        isDev && console.log(res.data);
                        this.integralMallDetail = res.data.couponDetail
                        this.perReceiveCondition = res.data.perReceiveCondition
                    } else {
                        console.log(res);
                    }
                }).catch(error => {
                    console.log(this.error);
                })
            },
            showPopUp() {
                this.popupShow = !this.popupShow
            },
            integralMallCreate() {
                if (!this.exchangeStatus) Toast.fail("已超过最大兑换次数")
                this.$api.integralmall.integralMallCreate({
                    activityCpId: this.activityCpId,
                    openId: this.token,
                    num: this.exchangeCount
                }).then(res => {
                    if (res.code == 0) {
                        console.log(res);
                        Toast.success('兑换成功!');
                        setTimeout(() => {
                            this.reload()
                        }, 2000)
                    } else {
                        Toast(res.msg);
                        console.log(res);
                    }
                    this.showPopUp()
                }).catch(err => {
                    console.log(err);
                    this.showPopUp()
                })

            }
        }
    }
</script>

<style lang="less">
    @import "../../assets/less/base";

    .van-stepper__input[disabled] {
        color: #7d7e80 !important;
    }

    .integral-detail {
        padding: 10px;
        min-height: 100%;
        background-color: @main-background-color;
        .pic-img {
            height: 200px;
            overflow-y: hidden;
            img {
                width: 100%;
            }
        }
        .detail-content-wrapper {
            .info-wrapper {
                .coupon-name {
                    margin: 10px 0;
                    color: @front-font-color;
                    font-size: 20px;
                    font-weight: 700;
                }
                .coupon-price {
                    color: @front-font-color;
                    text-decoration: line-through;
                    .fz(14px)
                }
                .need-integral {
                    height: 44px;
                    line-height: 44px;
                    /*padding-left: 10px;*/
                    /*color: #627e94;*/
                    /*font-size: 24px;*/
                    .fz();
                    color: #ffa459;
                }
                .exchange-btn {
                    text-align: right;
                    padding-right: 20px;
                    button {
                        background-color: @main-theme-color;
                        border: 1px solid @main-theme-color;
                        padding: 0 30px;
                    }
                }
                .receive-num {
                    text-align: right;
                    padding-right: 40px;
                    color: @front-font-color;
                    font-size: 14px;
                    font-style: italic;
                }
            }

            .rule-wrapper {
                margin-top: 20px;
                .date-text {
                    color: @front-font-color;
                    background: url("/images/weixin2/hbg1.png") no-repeat;
                    background-size: contain;
                    padding-left: 6px;
                }
                .date-range {
                    color: @front-font-color;
                }
                .rules-text {
                    color: @front-font-color;
                    background: url("/images/weixin2/hbg1.png") no-repeat;
                    background-size: contain;
                    padding-left: 6px;
                }
                .rules-info {
                    color: @front-font-color;
                    p {
                        margin-top: 0;
                        margin-bottom: 0;
                    }
                }
                .van-row {
                    margin-bottom: 20px;
                }
            }
        }
        .integral-cart-wrapper {
            height: 300px;
            background-color: #f1f5f8;
            color: @front-font-color;
            .title-wrapper {
                position: relative;
                background-color: #ffffff;
                height: 40px;
                .title {
                    height: 40px;
                    line-height: 40px;
                    font-size: 20px;
                    text-align: center;
                    color: @front-font-color;
                }
                .cross-btn {
                    position: absolute;
                    top: 0;
                    right: 20px;
                    height: 40px;
                    line-height: 40px;
                    font-size: 20px;
                    color: @main-theme-color;
                }
            }
            .info-wrapper {
                margin: 10px 0;
                .logo {
                    margin-left: 6px;
                    width: 90px;
                    height: 90px;
                    img {
                        width: 90px;
                        height: 90px;
                    }
                }
                .name {
                    padding-left: 20px;
                }
            }
            .buy-num-wrapper {
                background-color: #ffffff;
                padding: 20px 0;
                .text {
                    font-size: 16px;
                    padding-left: 15px;
                }
                .count {
                    text-align: center;
                }
            }
            .buy-btn-wrapper {
                margin-top: 10px;
                text-align: center;
                button {
                    background-color: @main-theme-color;
                    border: 1px solid @main-theme-color;
                    padding: 0 60px;
                }
            }
        }
    }
</style>
